package gui;

import javax.swing.JPanel;

import common.ChatIF;
import javax.swing.JLabel;

import java.awt.Color;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JCheckBox;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.UIManager;
/**
 * 
 * @author Idan Mann
 * This GUI Class presents the Chairman with the options to decide 
 * what should be done with the request as followed by the assessment.
 *
 */
public class VerdictPanel  extends JPanel implements ChatIF
{
	private JTextField requestIDtextField;
	private JButton btnApprove;
	private JButton btnRequireMoreInformation;
	private JButton btnDeny;
	private JTextArea textArea;
	private JCheckBox chckbxIRequireMore;
	private AcademicFrame frame;
	private ReviewAndDecisionPanel previousPanel;
	private String requestID;
	private JButton btnCancel_1;
	private CommitteeMemberPanel committeePanel;
	private String firstAndLastName;
	

	
	VerdictPanel(AcademicFrame frame, ReviewAndDecisionPanel previousPanel, String requestID, CommitteeMemberPanel committeePanel, String firstAndLastName)
	{
		this.frame = frame;
		this.previousPanel = previousPanel;
		this.requestID = requestID;
		this.firstAndLastName = firstAndLastName;
		this.committeePanel = committeePanel;  
		
		initialize();
		
		requestIDtextField.setText(requestID);
		
	}
	
		private void initialize()
		{
			
		setLayout(null);
		
		JLabel lblVerdict = new JLabel("Verdict for request number");
		lblVerdict.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblVerdict.setBounds(138, 31, 252, 14);
		add(lblVerdict);
		
		requestIDtextField = new JTextField();
		requestIDtextField.setEditable(false);
		requestIDtextField.setBounds(362, 30, 67, 20);
		add(requestIDtextField);
		requestIDtextField.setColumns(10);
		
		btnApprove = new JButton("Approve");
		btnApprove.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e) 
			{
				ArrayList<Object> approveRequest = new ArrayList<Object>();
				approveRequest.add("approveRequest");
				approveRequest.add(requestID);
				AcademicFrame.myclient.sendMessageToServer(approveRequest);
			}
		});
		btnApprove.setBounds(78, 206, 91, 23);
		add(btnApprove);
		
		btnDeny = new JButton("Deny");
		btnDeny.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				ArrayList<Object> denyRequest = new ArrayList<Object>();
				denyRequest.add("denyRequest");
				denyRequest.add(requestID);
				AcademicFrame.myclient.sendMessageToServer(denyRequest);				
			}
		});
		btnDeny.setBounds(457, 206, 91, 23);
		add(btnDeny);
		
		btnRequireMoreInformation = new JButton("Require more information");
		btnRequireMoreInformation.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if (textArea.getText().isEmpty())
				{
					(new Dialog("Please describe the additional information you require.")).setVisible(true);
				}
				else
				{
					ArrayList<Object> moreInformationIsRequired = new ArrayList<Object>();
					moreInformationIsRequired.add("moreInformationIsRequired");
					moreInformationIsRequired.add(requestID);
					moreInformationIsRequired.add(textArea.getText());
					AcademicFrame.myclient.sendMessageToServer(moreInformationIsRequired);	
				}
			}
		});
		btnRequireMoreInformation.setBounds(214, 206, 197, 23);
		add(btnRequireMoreInformation);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(78, 102, 485, 77);
		add(scrollPane);
		
		textArea = new JTextArea();
		textArea.setBackground(UIManager.getColor("TextArea.disabledBackground"));
		textArea.setEditable(false);
		scrollPane.setViewportView(textArea);
		
		chckbxIRequireMore = new JCheckBox("I require more information");
		chckbxIRequireMore.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0)
			{
				if (chckbxIRequireMore.isSelected())
				{
					textArea.setBackground(Color.white);
					btnApprove.setVisible(false);
					btnDeny.setVisible(false);
					btnRequireMoreInformation.setVisible(true);
					textArea.setEditable(true);
				}
				else
				{
					btnApprove.setVisible(true);
					btnDeny.setVisible(true);
					btnRequireMoreInformation.setVisible(false);
					
					float[] fakeHSB = {0,0,0};
					textArea.setText("");
					textArea.setEditable(false);
					textArea.setBackground(new Color (238, 238, 238));
					
				}
					
			}
		});
		
		chckbxIRequireMore.setBounds(72, 69, 180, 23);
		add(chckbxIRequireMore);
		
		btnCancel_1 = new JButton("Cancel");
		btnCancel_1.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e) 
			{
				frame.setContentPane(previousPanel);
				frame.setSize(640, 540);
			}
		});
		btnCancel_1.setBounds(261, 251, 91, 23);
		add(btnCancel_1);
		
		btnRequireMoreInformation.setVisible(false);
	}

	@Override
	public void display(Object message)
	{
		ArrayList<Object> answer = new ArrayList<Object>();
		answer = (ArrayList<Object>)message;
		
		if (	((String)(answer.get(0))).equalsIgnoreCase("changeStatus"))
		{
			
			ReviewAndDecisionPanel newRnDPanel = new ReviewAndDecisionPanel(frame,committeePanel,firstAndLastName,true);
			frame.setContentPane(newRnDPanel);
			frame.setSize(640, 540);
			
			ArrayList<Object> getListOfRequestsForReview = new ArrayList<Object>();
			getListOfRequestsForReview.add("getListOfRequestsForReview");	
			AcademicFrame.myclient.sendMessageToServer(getListOfRequestsForReview);
			
		}
		
	}
}
